/*
 * CameraLauncher.cpp
 *
 *  Created on: 2011. 3. 15.
 *      Author: JeeHoon
 */

/*
 * getPicture Options
 * quality [0, 100] (Default : 75)
 * allowEdit boolean (Default : true)
 * sourceType PHOTOLIBRARY:0, CAMERA:1, SAVEDPHOTOALBUM:2 (Default : CAMERA)
 * destinationType DATA_URL:0, FILE_URI:1 (Default : DATA_URL)
 */
#include "plugin/CameraLauncher.h"
#include "api/PluginManager.h"
#include "api/PluginResult.h"

#include <FIo.h>

using namespace Osp::Base;

CameraLauncher::CameraLauncher()
{
}

CameraLauncher::~CameraLauncher()
{
}

result
CameraLauncher::OnConstruct(PluginManager * pPluginManager, Osp::Base::String pluginName)
{
	result r = E_SUCCESS;

	return r;
}

result
CameraLauncher::execute(Osp::Base::String action, Osp::Base::String args, Osp::Base::String callbackId)
{
	result r = E_SUCCESS;

	Osp::Base::Collection::ArrayList* pDataList = NULL;
	String* pData = NULL;
	String* pData2 = NULL;
	Osp::App::AppControl* pAc = NULL;
	int quality = 75;
	bool allowEdit = true;
	PictureSourceType sourceType = CAMERA;


	if(action == L"getPicture")
	{
		if(getJSONObjectItem(args,L"quality") != L"")
			Integer::Parse(getJSONObjectItem(args,L"quality"),quality);

		if(getJSONObjectItem(args,L"allowEdit") == L"false")
			allowEdit = false;

		if(getJSONObjectItem(args,L"sourceType") == L"0")
			sourceType = PHOTOLIBRARY;
		else if(getJSONObjectItem(args,L"sourceType") == L"2")
			sourceType = SAVEDPHOTOALBUM;

		if( sourceType == CAMERA)
		{
			if(E_SUCCESS == r)
				r = enqueueParameter(callbackId,args);

			if(E_SUCCESS == r)
			{
				pDataList = new Osp::Base::Collection::ArrayList();
				pData = new String(L"type:camera");
				r = pDataList->Construct();
			}

			if(E_SUCCESS == r)
			{
				r = pDataList->Add(*pData);
				pData = NULL;
			}


			if(E_SUCCESS == r)
			{
				pAc = Osp::App::AppManager::FindAppControlN(Osp::App::APPCONTROL_CAMERA, Osp::App::OPERATION_CAPTURE);
				r = GetLastResult();
			}

			if(E_SUCCESS == r)
			{
				pAc->Start(pDataList, this);
				delete pAc;
				pAc = NULL;
			}
		}
		else if(sourceType == PHOTOLIBRARY || sourceType == SAVEDPHOTOALBUM)
		{
			if(E_SUCCESS == r)
				r = enqueueParameter(callbackId,args);

			if(E_SUCCESS == r)
			{
				pDataList = new Osp::Base::Collection::ArrayList();
				pData = new String(L"type:image");
				pData2 = new String(L"selectionType:single");
				r = pDataList->Construct();
			}

			if(E_SUCCESS == r)
			{
				r = pDataList->Add(*pData);
				pData = NULL;
			}

			if(E_SUCCESS == r)
			{
				r = pDataList->Add(*pData2);
				pData2 = NULL;
			}

			if(E_SUCCESS == r)
			{
				pAc = Osp::App::AppManager::FindAppControlN(Osp::App::APPCONTROL_MEDIA, Osp::App::OPERATION_PICK);
				r = GetLastResult();
			}

			if(E_SUCCESS == r)
			{
				pAc->Start(pDataList, this);
				delete pAc;
				pAc = NULL;
			}
		}
	}
	else
	{
		printLog(L"Unknown action: "+action);
	}

	if(NULL != pDataList)
	{
		pDataList->RemoveAll(true);
		delete pDataList;
		pDataList = NULL;
	}

	if(NULL != pData)
	{
		delete pData;
		pData = NULL;
	}

	if(NULL != pData2)
	{
		delete pData2;
		pData2 = NULL;
	}

	return r;
}

void
CameraLauncher::OnAppControlCompleted (const Osp::Base::String &appControlId,const Osp::Base::String &operationId,const Osp::Base::Collection::IList *pResultList)
{
	result r = E_SUCCESS;
	String callBackID,args;
	PluginResult pluginResult;
	String * pOutputResult = NULL;
	String * pOutputFilePath = NULL;

	dequeueParameter(callBackID,args);

	if(appControlId == Osp::App::APPCONTROL_CAMERA || appControlId == Osp::App::APPCONTROL_MEDIA)
	{
		if(E_SUCCESS == r)
		{
			pOutputResult = (String *)pResultList->GetAt(0);
			r = GetLastResult();
		}
	}
	else
	{
		pluginResult.inti(ERROR,L"Camera result invalid data");
		r = E_INVALID_DATA;
	}

	if(E_SUCCESS == r)
	{
		if(*pOutputResult == Osp::App::APPCONTROL_RESULT_SUCCEEDED)
		{
			pOutputFilePath = (String *)pResultList->GetAt(1);
			r = GetLastResult();
		}
		else if(*pOutputResult == Osp::App::APPCONTROL_RESULT_CANCELED)
		{
			pluginResult.inti(ERROR,L"Camera result canceled");
			r = E_FAILURE;
		}
		else if(*pOutputResult == Osp::App::APPCONTROL_RESULT_FAILED)
		{
			pluginResult.inti(ERROR,L"Camera result failed");
			r = E_FAILURE;
		}
		else
		{
			pluginResult.inti(ERROR,L"Camera result unknown");
			r = E_FAILURE;
		}
	}

	if(E_SUCCESS == r)
	{
		DestinationType destinationType = DATA_URL;
		if(getJSONObjectItem(args,L"destinationType") == L"1") destinationType = FILE_URI;

		if(DATA_URL == destinationType)
		{
			String encodedData;
			Osp::Base::ByteBuffer capturedData;
			Osp::Io::File file;
			int size = 0;

			file.Construct(*pOutputFilePath,L"rb");
			file.Seek(Osp::Io::FILESEEKPOSITION_END,0);
			size = file.Tell();
			file.Seek(Osp::Io::FILESEEKPOSITION_BEGIN,0);
			capturedData.Construct(size);
			file.Read(capturedData);

			capturedData.SetPosition(0);

			Utility::StringUtil::EncodeToBase64String(capturedData,encodedData);
			pluginResult.inti(OK,encodedData);
		}
		else
		{
			String destFilePath = L"/Home/pic.jpg";

			r = Osp::Io::File::Copy(*pOutputFilePath,destFilePath,false);
			if(E_SUCCESS != r)
			{
				printLog(L"Move Fail."+ String(GetErrorMessage(r)));
			}

			pluginResult.inti(OK,L"file://" + destFilePath);
		}
	}

	if(E_SUCCESS == r)
	{
		__pPluginManager->evaluateJavascript(pluginResult.toSuccessCallbackString(callBackID));
	}
	else
	{
		__pPluginManager->evaluateJavascript(pluginResult.toErrorCallbackString(callBackID));
	}
}



